package com.gj.demojuc.chap03;
//资源类
//class ShareDataOne{
//    private int data = 0;
//
//    public synchronized void increment() throws InterruptedException {
////        if (data != 0){
////            //等待唤醒
////            wait();
////        }
//        while (data != 0){
//            wait();
//        }
//        data++;
//        System.out.println(Thread.currentThread().getName() + data);
//        //通知
//        notifyAll();
//    }
//    public synchronized void decrement() throws InterruptedException {
////        if (data <= 0){
////            //等待
////            wait();
////        }
//        while (data != 1){
//            wait();
//        }
//        //干活
//        data--;
//        System.out.println(Thread.currentThread().getName() + data);
//        //通知
//        notifyAll();
//    }
//}

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 两个线程操作一个初始值为0的变量，实现一个线程对变量增加1，一个线程对变量减少1，交替10轮。
 * wait notify notifyall
 * 多线程编程代码模版
 * 线程操作资源类
 * 判断
 * 干活
 * 通知
 */

class ShareDataOne{
    private int data = 0;
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void increment() throws InterruptedException {
        lock.lock();
        try {
            while (data != 0){
                condition.await();
            }
            data++;
            System.out.println(Thread.currentThread().getName() + data);
            //通知
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            while (data != 1){
                condition.await();
            }
            //干活
            data--;
            System.out.println(Thread.currentThread().getName() + data);
            //通知
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
}

public class WaitNotifyDemo {
    public static void main(String[] args) {
        ShareDataOne shareDataOne = new ShareDataOne();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    shareDataOne.increment();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    shareDataOne.decrement();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    shareDataOne.increment();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"C").start();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    shareDataOne.decrement();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"D").start();
    }
}
